package com.itheima.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.dao.CategoryDao;
import com.itheima.domain.Category;
import com.itheima.domain.Dish;
import com.itheima.domain.Setmeal;
import com.itheima.exception.DeleteFenLeiException;
import com.itheima.service.CategoryService;
import com.itheima.service.DishService;
import com.itheima.service.SetmealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryDao, Category> implements CategoryService {
    @Autowired
    private DishService dishService;

    @Autowired
    private SetmealService setmealService;

    @Override
    public void erase(Long ids) {
        //查询该分类有没有关联菜品
        //1.1由数据库可知，分类表的id==菜品表的cateporyid，
        LambdaQueryWrapper<Dish> lqw1 = new LambdaQueryWrapper<>();
        lqw1.eq(Dish::getCategoryId,ids);
        int count1 = dishService.count(lqw1);

        if(count1 > 0){
            throw new DeleteFenLeiException("该分类关联了菜品，无法删除");
        }
        //查询该分类有没有关联套餐
        //1.2由数据库可知，分类表的id==套餐表的cateporyid，
        LambdaQueryWrapper<Setmeal> lqw2 = new LambdaQueryWrapper<>();
        lqw2.eq(Setmeal::getCategoryId,ids);
        int count2 = setmealService.count(lqw2);

        if(count2 > 0){
            throw new DeleteFenLeiException("该分类关联了套餐，无法删除");
        }

        super.removeById(ids);
    }
}
